#pragma once

#include<vector>
#include<iostream>
#include<algorithm>

namespace bit
{
	template<class T>
	class Less
	{
	public:
		bool operator()(T x, T y)const
		{
			return x < y;
		}
	};
	template<class T>
	class Greater
	{
	public:
		bool operator()(T x, T y)
		{
			return x > y;
		}
	};
	template<class T,class Container=std::vector<T>,class Compare = Less<T>>
	class priority_queue
	{
	public:
		template<class InputIterator>
		priority_queue(InputIterator frist,InputIterator last)
			:_con(frist,last)
		{
			for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
			{
				adjust_under(i);
			}
		}
		priority_queue() {}
		void adjust_up(int child)
		{
			Compare com;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (com(_con[parent], _con[child]))
				{
					std::swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
					break;
			}
		}
		void adjust_under(int parent)
		{
			Compare com;
			int child = 2 * parent + 1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size() && com(_con[child] , _con[child + 1]))
				{
					++child;
				}

				if (com(_con[parent], _con[child]))
				{
					std::swap(_con[child], _con[parent]);
					parent = child;
					child = 2 * parent + 1;
				}
				else
					break;
				
			}
		}
		void push(const T& x)
		{
			_con.push_back(x);
			adjust_up(_con.size() - 1);
		}
		void pop()
		{
			std::swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjust_under(0);

		}
		const T& top()
		{
			return _con[0];
		}
		bool empty()
		{
			return _con.size() == 0;
		}
		size_t size()const
		{
			return _con.size();
		}

	private:
		Container _con;
	};
}